#include "StdAfx.h"
#include "SaveLoad.h"

CSaveLoad::CSaveLoad(void)
{
}

CSaveLoad::~CSaveLoad(void)
{
}

void CSaveLoad::SaveNodeList(CArchive& ar,CShapeObject* rootNode)
{		
	CNodeList lchildlist = rootNode->getChildList();				

	ar << rootNode->getChildList().size();
	for(CNodeListItr pnode = lchildlist.begin();pnode != lchildlist.end();pnode++)
	{
		m_nodeQueue.push_back(*pnode);	
		switch((*pnode)->getType())
		{
		case ST_RECTANGLE:	
			{
				ar << ST_RECTANGLE;
				CRectangle* pRect = (CRectangle*)(*pnode);
				ar << pRect->getLeftTopPoint();
				ar << pRect->getRightBottomPoint();	
				for(int i = 0; i < 4; i++)
				{
					ar << pRect->getLinkedNodes()[i].getPosition();
				}				
				break;
			}
		case ST_DIAMOND:
			{
				ar << ST_DIAMOND;
				CDiamond* pDiamond = (CDiamond*)(*pnode);
				ar << pDiamond->getLeftTopPoint();
				ar << pDiamond->getRightBottomPoint();	
				for(int i = 0; i < 4; i++)
				{
					ar << pDiamond->getLinkedNodes()[i].getPosition();
				}
				break;
			}
		case ST_ARROW:
			{
				ar << ST_ARROW;
				CArrow* pArrow = (CArrow*)(*pnode);
				ar << pArrow->getStartPoint().getPosition();
				ar << pArrow->getEndedPoint().getPosition();
				ar << pArrow->getTurningPointList().size();
				CLine::PointList pointList = pArrow->getTurningPointList();
				CLine::PointList::iterator pointListItr = pointList.begin();
				for(; pointListItr != pointList.end(); pointListItr++)
				{
					ar << (*pointListItr);
				}
				break;
				
			}
		case ST_TEXT:
			{
				ar << ST_TEXT;
				CText* pText = (CText*)(*pnode);
				ar << pText->getLeftTopPoint();
				ar << pText->getRightBottomPoint();
				ar << pText->getTextInfo().getTextFont().cbSize;
				ar << pText->getTextInfo().getTextFont().dwMask;
				ar << pText->getTextInfo().getTextFont().dwEffects;
				ar << pText->getTextInfo().getTextFont().yHeight;
				ar << pText->getTextInfo().getTextFont().yOffset;
				ar << pText->getTextInfo().getTextFont().crTextColor;
				ar << pText->getTextInfo().getTextFont().bCharSet;
				ar << pText->getTextInfo().getTextFont().bPitchAndFamily;
				for(int i = 0; i < LF_FACESIZE ; i++)
				{
					ar << pText->getTextInfo().getTextFont().szFaceName[i];
				}
				long textListSize = pText->getTextInfo().getTextContent().size();
				ar << textListSize;
				std::vector<CString> pTextList = pText->getTextInfo().getTextContent();
				std::vector<CString>::iterator pTextItr = pTextList.begin();				
				for(long i = 0; i < textListSize; i++) 
				{
					ar << (*pTextItr);
				}				
				break;
			}
		default:
			{
				break;
			}
		}
	}


	CShapeObject* ptempNode;
	while((ptempNode = GetNextNode()) != NULL)
	{			
		ar << ptempNode->getChildList().size();

		CNodeList ltchildlist = ptempNode->getChildList();
		if(!ltchildlist.empty())
		{
			for(CNodeListItr ptnode = ltchildlist.begin();ptnode != ptempNode->getChildList().end();ptnode++)
			{
				m_nodeQueue.push_back(*ptnode);
				switch((*ptnode)->getType())
				{
				case ST_RECTANGLE:	
					{
						ar << ST_RECTANGLE;
						CRectangle* pRect = (CRectangle*)(*ptnode);
						ar << pRect->getLeftTopPoint();
						ar << pRect->getRightBottomPoint();	
						for(int i = 0; i < 4; i++)
						{
							ar << pRect->getLinkedNodes()[i].getPosition();
						}
						break;
					}
				case ST_DIAMOND:
					{
						ar << ST_DIAMOND;
						CDiamond* pDiamond = (CDiamond*)(*ptnode);
						ar << pDiamond->getLeftTopPoint();
						ar << pDiamond->getRightBottomPoint();	
						for(int i = 0; i < 4; i++)
						{
							ar << pDiamond->getLinkedNodes()[i].getPosition();
						}
						break;
					}
				case ST_ARROW:
					{
						ar << ST_ARROW;
						CArrow* pArrow = (CArrow*)(*ptnode);
						ar << pArrow->getStartPoint().getPosition();
						ar << pArrow->getEndedPoint().getPosition();
						ar << pArrow->getTurningPointList().size();
						CLine::PointList pointList = pArrow->getTurningPointList();
						CLine::PointList::iterator pointListItr = pointList.begin();
						for(; pointListItr != pointList.end(); pointListItr++)
						{
							ar << (*pointListItr);
						}
						break;

					}
				case ST_TEXT:
					{
						ar << ST_TEXT;
						CText* pText = (CText*)(*ptnode);
						ar << pText->getLeftTopPoint();
						ar << pText->getRightBottomPoint();
						ar << pText->getTextInfo().getTextFont().cbSize;
						ar << pText->getTextInfo().getTextFont().dwMask;
						ar << pText->getTextInfo().getTextFont().dwEffects;
						ar << pText->getTextInfo().getTextFont().yHeight;
						ar << pText->getTextInfo().getTextFont().yOffset;
						ar << pText->getTextInfo().getTextFont().crTextColor;
						ar << pText->getTextInfo().getTextFont().bCharSet;
						ar << pText->getTextInfo().getTextFont().bPitchAndFamily;
						for(int i = 0; i < LF_FACESIZE ; i++)
						{
							ar << pText->getTextInfo().getTextFont().szFaceName[i];
						}
						long textListSize = pText->getTextInfo().getTextContent().size();
						ar << textListSize;
						std::vector<CString> pTextList = pText->getTextInfo().getTextContent();
						std::vector<CString>::iterator pTextItr = pTextList.begin();				
						for(long i = 0; i < textListSize; i++) 
						{
							ar << (*pTextItr);
						}							
						break;
					}
				default:
					{
						break;
					}
				}
			}
		}			
	}

	ar << -1;
	
}

void CSaveLoad::LoadNodeList(CArchive& ar,CShapeObject* rootNode)
{
	CPoint startPoint;
	CPoint endPoint;
	CPoint temPoint;
	CShapeObject* pCurrentNode = rootNode;
	long iObjNum;
	ar >> iObjNum;

	while(iObjNum != -1)		
	{		
		int iObjType;
		for(int i=0;i<iObjNum;i++)
		{
			ar >> iObjType;
			switch(iObjType)
			{
			case ST_RECTANGLE:
				{
					ar >> startPoint;
					ar >> endPoint;
					CRectangle* rect = new CRectangle(startPoint,endPoint);
					for(int i=0; i<4; i++)
					{
						ar >> temPoint;
						rect->getLinkedNodes()[i].setPosition(temPoint);
					}					
					pCurrentNode->getChildList().push_back(rect);
					break;
				}
			case ST_DIAMOND:
				{
					ar >> startPoint;
					ar >> endPoint;
					CDiamond* diamond= new CDiamond(startPoint,endPoint);
					for(int i=0; i<4; i++)
					{
						ar >> temPoint;
						diamond->getLinkedNodes()[i].setPosition(temPoint);
					}					
					pCurrentNode->getChildList().push_back(diamond);
					break;
				}
			case ST_ARROW:
				{
					ar >> startPoint;
					ar >> endPoint;
					CArrow* arrow = new CArrow();
					CEndPoint* pStartPoint = new CEndPoint();
					CEndPoint* pEndPoint = new CEndPoint();
					pStartPoint->setPosition(startPoint);
					pEndPoint->setPosition(endPoint);
					arrow->setStartPoint(*pStartPoint);
					arrow->setEndedPoint(*pEndPoint);
					int pointNum;
					ar >> pointNum;
					for(int i = 0; i<pointNum; i++)
					{
						ar >> temPoint;
						CPoint* point = new CPoint(temPoint.x,temPoint.y);
						arrow->getTurningPointList().push_back(*point);
					}
					arrow->finishCreated();
					pCurrentNode->getChildList().push_back(arrow);
					break;
				}
			case ST_TEXT:
				{
					ar >> startPoint;
					ar >> endPoint;
					CText* text = new CText(startPoint,endPoint);
					CTextInfo textInfo;
					CHARFORMAT charFormat;
					ar >> charFormat.cbSize;
					ar >> charFormat.dwMask;
					ar >> charFormat.dwEffects;
					ar >> charFormat.yHeight;
					ar >> charFormat.yOffset;
					ar >> charFormat.crTextColor;
					ar >> charFormat.bCharSet;
					ar >> charFormat.bPitchAndFamily;
					for(int i = 0; i < LF_FACESIZE ; i++)
					{
						ar >> charFormat.szFaceName[i];
					}
					textInfo.setTextFont(charFormat);
					
					long lineNum;
					ar >> lineNum;
					CString content;
					for(long i = 0; i < lineNum; i++)
					{
						ar >> content;
						textInfo.addTextContentLine(content);
					}
					text->setTextInfo(textInfo);
					pCurrentNode->getChildList().push_back(text);
					break;
				}
			default:
				break;
			}
		}	
		pCurrentNode = GetNextNode();
		ar >> iObjNum;
	}
}

CShapeObject* CSaveLoad::GetNextNode()
{
	if(!m_nodeQueue.empty())
	{		
		CNodeListItr pnode = m_nodeQueue.begin();	
		CShapeObject* ptnode = *pnode;
		m_nodeQueue.pop_front();	

		return ptnode;
	}
	else
	{
		return NULL;
	}	
}

void CSaveLoad::EstablishRelation(CShapeObject* rootNode)
{
	//CNodeList lChildList = rootNode->getChildList();
	//for(CNodeListItr pNodeItr = lChildList.begin();
	//	pNodeItr != lChildList.end();
	//	pNodeItr++)
	//{
	//	if((*pNodeItr)->getType() != ST_ARROW)
	//	{
	//		continue;
	//	}
	//	else
	//	{

	//	}
	//}
}